<?php
if(!defined('IN_ROOT') || !isset($php_self) || !preg_match("/[\/\\\\]index\.php$/", $php_self)) {
	exit('Access Denied');
}

//权限检查
permission();

// 检查站点名字是否符合逻辑
function checksitename($sitename) {
	if(!$sitename || strlen($sitename) > 30) {
		$result = '站点名不能空并不能大于30个字符<br />';
		return $result;
	}
	elseif(eregi("[<>{}(),%#|^&!`$]",$sitename)) {
		$result = '站点名中不能含有特殊字符<br />';
		return $result;
	}
}

// 检查站点描述是否符合逻辑
function checknote($note = '') {
	if($note && strlen($note) > 200) {
		$result = '站点描述不能大于200个字符<br />';
		return $result;
	}
}
//修改FEED

if($action == 'domodsite') {
	$site_title = trim($_POST["title"]);
  	$site_url = trim($_POST["url"]);
  	$site_rss_url = trim($_POST["rss_url"]);
  	$site_icon = trim($_POST["icon"]);
  	$rss_cate = trim($_POST["rss_cate"]);
  	$site_email = trim($_POST["email"]);
  	$site_count = trim($_POST["count"]);
  	$site_content = trim($_POST["content"]);
  	$state = intval($_POST['state']);
	$cid    = intval($_POST['cid']);
	$result = checksitename($site_title);

	if($result)	{redirect($result);}

	$site_title = char_cv($site_title);
	$rs = $db->fetch_one_array("SELECT count(*) AS categories FROM {$tablepre}xna_category WHERE cid!='$cid' AND cate_title='$site_title'");
    if($rs['categories']) {
		redirect('已经有其他站点使用'.$site_title.'这个名称');
    }

	// 更新站点
   $db->query("UPDATE {$tablepre}xna_site SET
		    site_title='$site_title' ,
		    site_url='$site_url' ,
		    rss_url='$site_rss_url' ,
		    site_icon='$site_icon' ,
		    rss_cate='$rss_cate',
		    site_email='$site_email',
		    site_count='$site_count',
		    site_content='$site_content',
		    state='state'
		WHERE siteid='$siteid'");
	categories_recache();

    redirect('修改站点成功', 'index.php?job=site');
}

//批量处理

if($_POST['action'] == 'domorefeed') {
	if($ids = implode_ids($_POST['delete'])) {
		$query = "SELECT feedid FROM {$tablepre}xna_feed WHERE feedid IN ($ids)";
		$tatol = $db->num($db->query($query));
		$db->query("DELETE FROM {$tablepre}xna_feed WHERE feedid IN ($ids)");
		$db->query("UPDATE {$tablepre}xna_statistics SET feed_count=feed_count-'$tatol'");
		statistics_recache();
		sites_recache();
	}
	if(is_array($_POST['displayorder'])) {
		foreach($_POST['displayorder'] as $feedid => $value) {
			$db->query("UPDATE {$tablepre}xna_feed SET displayorder='".intval($_POST['displayorder'][$feedid])."', state='".intval($_POST['state'][$feedid])."' WHERE feedid='".intval($feedid)."'");
		}
	}
    redirect('Feed已成功更新', 'index.php?job=feed');
}


if(!$action) {
	$action = 'list';
}
//站点操作
if (in_array($action, array('modfeed', 'delcate'))) {
	if ($action == 'modfeed') {
		$subnav = '编辑Feed';
        $feed = $db->fetch_one_array("SELECT * FROM {$tablepre}xna_feed WHERE feedid='".intval($_GET['id'])."'");
        $feed[addtime] = date("Y-m-d h:m",$feed["addtime"]);
	} else {
			$subnav = '删除Feed';
	}
}

if ($action == 'list') {
	if($page) {
		$start_limit = ($page - 1) * 30;
	} else {
		$start_limit = 0;
		$page = 1;
	}
	$tatol = $stats['feed_count'];	
	$query = $db->query("SELECT * FROM {$tablepre}xna_feed ORDER BY addtime DESC LIMIT $start_limit, 30");
	$multipage = multi($tatol, 30, $page, 'index.php?job=feed&action=list'.$pagelink);
	$feeddb = array();
	while ($feed = $db->row($query)) {
		if ($feed['state'] == '1') {
			$feed['state'] = '<option value="1" selected>显示</option><option value="0">隐藏</option><option value="2">固顶</option>';
		} elseif($feed['state'] == '0') {
			$feed['state'] = '<option value="1">显示</option><option value="0" selected>隐藏</option><option value="2">固顶</option>';
		}else{
			$feed['state'] = '<option value="1">显示</option><option value="0">隐藏</option><option value="2" selected>固顶</option>';
		}
		$feed[addtime] = date("Y-m-d h:m",$feed["addtime"]);
		$feeddb[] = $feed;
	}
	unset($site);
	$db->free($query);
	$subnav = '编辑Feed';
}

$navlink_L = ' &raquo; <a href="index.php?job=site">站点管理</a>'.($subnav ? ' &raquo; '.$subnav : '');
cpheader();
include template("feed", _ROOT . "/template/admin/");

?>